package com.linkage.jz.bankruptcy.utils;

import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.linkage.jz.bankruptcy.base.BaseCreateActivity;
import com.linkage.seapeak.pojo.UploadImage;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import static com.linkage.seapeak.activity.user.ImageShowActivity.FROM_MUlTI_EDIT;

public class SelectPicHelpler<T> {

    private List<UploadImage> uploadedImages;
    private BaseCreateActivity.GridImagesAdapter adapter;
    private UploadImageConverter<T> mUploadImageConverter;
    private final Type mUploadImageType = new TypeToken<List<UploadImage>>() {}.getType();

    public SelectPicHelpler(BaseCreateActivity baseCreateActivity, List<T> list, UploadImageConverter<T> uploadImageConverter) {
        mUploadImageConverter = uploadImageConverter;
        init(baseCreateActivity, list);
    }

    private void init(BaseCreateActivity baseCreateActivity, List<T> tempList) {
        if (tempList == null) tempList = new ArrayList<>();
        uploadedImages = new ArrayList<>(tempList.size());
        for(T t : tempList) uploadedImages.add(mUploadImageConverter.toUploadImage(t));
        adapter = new BaseCreateActivity.GridImagesAdapter(baseCreateActivity, FROM_MUlTI_EDIT);
        adapter.setList(uploadedImages);
    }

    public void initView(RecyclerView recyclerView) {
        recyclerView.setAdapter(adapter);
    }

    public List<UploadImage> getUploadedImages() {
        return uploadedImages;
    }

    public String getUploadedImagesJson() {
        return new Gson().toJson(uploadedImages, mUploadImageType);
    }
}
